package erfgame.core.entity;

public abstract class AbstractStatefulPhysicalEntity 
	extends AbstractPhysicalEntity 
	implements StatefulEntity {

	private static final long serialVersionUID = 0;

	private long currentStateTime;
	private State currentState;
	
	public AbstractStatefulPhysicalEntity( long id, EntityType entityType ) {
		super( id, entityType );
	}
		
	public State getCurrentState() {
		return this.currentState;
	}
	
	public long getCurrentStateTime() {
		return this.currentStateTime;
	}
	
	public void setCurrentState( State currentState, long currentStateTime ) {
		State oldState = this.currentState;
		if( (oldState == null && currentState != null) || !oldState.equals( currentState ) ) {
			this.currentState = currentState;
			this.currentStateTime = currentStateTime;
			// TODO don't want to create a new object here
			this.fireEntityChangeEvent(new EntityChangeEvent<State>(EntityEventType.state, oldState, currentState));
		} else if( this.currentStateTime > currentStateTime ) {
			this.currentStateTime = currentStateTime;
			// TODO fire event?? 
		}
	}
}
